package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.adapter.MyAdapter;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.TaskItem1;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.NoScrollView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class TaskActivity extends AbsActivity {
    private EditText idSearchEdit;
    private TextView idOrderText;
    private NoScrollView gv;
    private NoScrollView gv2;
    private NoScrollView gv3;
    private NoScrollView gv4;
    private ImageView icBack;
    private TextView topTitle;
    private ArrayList<TaskItem1> data1 = new ArrayList<>();
    private ArrayList<TaskItem1> data2 = new ArrayList<>();//买
    private ArrayList<TaskItem1> data3 = new ArrayList<>();//送
    private ArrayList<TaskItem1> data4 = new ArrayList<>();//取

    private MyAdapter adapter1;
    private MyAdapter adapter2;
    private MyAdapter adapter3;
    private MyAdapter adapter4;
    private void initView() {
        idSearchEdit = (EditText)findViewById( R.id.id_search_edit );
        idOrderText = (TextView)findViewById( R.id.id_order_Text );
        gv = (NoScrollView)findViewById( R.id.gv );
        gv2 = (NoScrollView)findViewById( R.id.gv2 );
        gv3 = (NoScrollView)findViewById( R.id.gv3 );
        gv4 = (NoScrollView)findViewById( R.id.gv4 );
        icBack = findViewById(R.id.ic_back);
        icBack.setVisibility(View.VISIBLE);
        topTitle = findViewById(R.id.title);
        topTitle.setText("万能跑腿");
    }

    @Override
    public int setView() {
        return R.layout.activity_task;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
        requestData();
    }

    private void requestData() {
//        TaskItem1 taskItem1 = new TaskItem1();
//        taskItem1.setTitle("帮你买");
//        taskItem1.setTaskType(SubmitTaskActivity.TASK_BUY);
//        data1.add(taskItem1);
//        TaskItem1 taskItem2 = new TaskItem1();
//        taskItem2.setTitle("帮你送");
//        taskItem2.setTaskType(SubmitTaskActivity.TASK_DELIVERY);
//        data1.add(taskItem2);
//        TaskItem1 taskItem3 = new TaskItem1();
//        taskItem3.setTaskType(SubmitTaskActivity.TASK_FETCH);
//        taskItem3.setTitle("帮你取");
//        data1.add(taskItem3);
        NetTool.getApi().fetch().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<TaskItem1>>>() {
                    @Override
                    public void onData(BaseResponse<List<TaskItem1>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            data4.addAll(value.getData());
                            adapter4.notifyDataSetChanged();
                        }
                    }
                });

        NetTool.getApi().buy().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<TaskItem1>>>() {
                    @Override
                    public void onData(BaseResponse<List<TaskItem1>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            data2.addAll(value.getData());
                            adapter2.notifyDataSetChanged();
                        }
                    }
                });

        NetTool.getApi().delivery().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<TaskItem1>>>() {
                    @Override
                    public void onData(BaseResponse<List<TaskItem1>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            data3.addAll(value.getData());
                            adapter3.notifyDataSetChanged();
                        }
                    }
                });

        NetTool.getApi().get_banner().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<TaskItem1>>>() {
                    @Override
                    public void onData(BaseResponse<List<TaskItem1>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            data1.addAll(value.getData());
                            adapter1.notifyDataSetChanged();
                        }
                    }
                });
    }

    private void initLogic() {
        icBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        idOrderText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TaskActivity.this,SubmitTaskActivity.class));
            }
        });
        adapter1 = new MyAdapter<TaskItem1>(data1,R.layout.syg_mian_title_list) {
            @Override
            public void bindView(final ViewHolder holder, final TaskItem1 obj) {
                holder.setText(R.id.qs_title,obj.getTitle());
                holder.getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(mActivity,SubmitTaskActivity.class).putExtra(SubmitTaskActivity.Type,obj.getTaskType()));
                    }
                });
            }
        };
        adapter2 = new MyAdapter<TaskItem1>(data2,R.layout.task_buy_item) {
            @Override
            public void bindView(final ViewHolder holder, final TaskItem1 obj) {
                holder.setText(R.id.title_name,obj.getTitle());
                holder.setText(R.id.title_name01,obj.getDescription());
                Glide.with(mActivity).load(obj.getCover()).into((ImageView) holder.getView(R.id.title_img));
                holder.getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mActivity,SubmitTaskActivity.class);
                        intent.putExtra(SubmitTaskActivity.Type,SubmitTaskActivity.TASK_BUY);
                        intent.putExtra(SubmitTaskActivity.Id,data2.get(holder.getItemPosition()).getId());
                        startActivity(intent);
                        ToastUtil.showToast(obj.toString());
                    }
                });
            }
        };
        adapter3 = new MyAdapter<TaskItem1>(data3,R.layout.task_delivery_item) {
            @Override
            public void bindView(final ViewHolder holder, final TaskItem1 obj) {
                holder.setText(R.id.title_name,obj.getTitle());
                holder.setText(R.id.title_name01,obj.getDescription());
                Glide.with(mActivity).load(obj.getCover()).into((ImageView) holder.getView(R.id.title_img));
                holder.getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mActivity,SubmitTaskActivity.class);
                        intent.putExtra(SubmitTaskActivity.Type,SubmitTaskActivity.TASK_DELIVERY);
                        intent.putExtra(SubmitTaskActivity.Id,data3.get(holder.getItemPosition()).getId());
                        startActivity(intent);ToastUtil.showToast(obj.toString());
                    }
                });
            }
        };
        adapter4 = new MyAdapter<TaskItem1>(data4,R.layout.task_delivery_item) {
            @Override
            public void bindView(final ViewHolder holder, final TaskItem1 obj) {
                holder.setText(R.id.title_name,obj.getTitle());
                holder.setText(R.id.title_name01,obj.getDescription());
                Glide.with(mActivity).load(obj.getCover()).into((ImageView) holder.getView(R.id.title_img));
                holder.getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mActivity,SubmitTaskActivity.class);
                        intent.putExtra(SubmitTaskActivity.Type,SubmitTaskActivity.TASK_FETCH);
                        intent.putExtra(SubmitTaskActivity.Id,data4.get(holder.getItemPosition()).getId());
                        startActivity(intent);ToastUtil.showToast(obj.toString());
                    }
                });
            }
        };
        gv4.setAdapter(adapter4);
        gv3.setAdapter(adapter3);
        gv2.setAdapter(adapter2);
        gv.setAdapter(adapter1);
    }
}
